(function(){

'use strict';
/**
 * customer 吴尧
 * @ngdoc function
 * @name sasdApp.controller:valueAnalysisController
 * @description 客户发展分析控制器
 * # trafficAnalysisController
 * Controller of the sasApp
 */
angular.module('controllers.oldOMDisplay.infoDispaly')
  .controller('valueAnalysisController',valueAnalysisController);

function valueAnalysisController($scope,InfoViewService,COREDATA,CoreService,CoreLoading){

	$scope.shoumun = 1;
	$scope.provinces =COREDATA.PROVINCES;
	$scope.timeTypes = COREDATA.TIMETYPES; //帐期数据
	$scope.translate = {
		provinceCode: [],
		businessCode: [],
		timeTypeCode: {timeTypeCode: '1',timeTypeName: '日'},
		startTime: new Date(),
		startQuarter: { code: '1', quarterName: '第一季度'},
		endTime: new Date(),
		endQuarter: { code: '1', quarterName: '第一季度'},
		year: {
			startTime: '20141',
	     endTime:'20152'
		}
	};

	$scope.$watch('translate.timeTypeCode',function(newValue){
		var month = new Date().getMonth();                  //当前月份
		var day = new Date().getDate();                     //当前天
		var year = new Date().getFullYear();                //当前年份
		switch (newValue.timeTypeCode) {
			case '1':
				$scope.translate.startTime = new Date(year, month, day - 1);
				$scope.translate.endTime = new Date(year, month, day - 1);
				break;
			case '3':
				$scope.translate.startTime = new Date(year, month);
				$scope.translate.endTime = new Date(year, month);
				break;
			case '4':
				$scope.translate.year.startTime = '20141';
				$scope.translate.year.endTime = '20152';
				break;
			case '5':
				$scope.translate.year.startTime = Number(new Date().getFullYear());
				$scope.translate.year.endTime = Number(new Date().getFullYear());
		}
	});


	$scope.searchValue = function(){
		var paramsData = {};
		paramsData.provinceCodes = $scope.translate.provinceCode.map(function(item){
			return item.provinceCode;
		});
		paramsData.timeType = '4';
		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		var start = splitTime($scope.translate.year.startTime);
		var end = splitTime($scope.translate.year.endTime);
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			paramsData.startTime = start.year;
			paramsData.startQuarter = start.quarter;
			paramsData.endTime = end.year;
			paramsData.endQuarter = end.quarter;
			InfoViewService.queryValue(paramsData,searchValue);
		}

	};

	/**
	 * 季度字符串,解析为年度和季度字段
	 * @param quarterString 季度字符串
	 * @returns {{}} 返回季度，年份的对象
	 */
	function splitTime(quarterString){
		var yearQuarter ={};
		var year = quarterString.substring(0,4);
		var quarter = quarterString.substring(4,5);
		yearQuarter.year = year;
		yearQuarter.quarter = quarter;
		return yearQuarter;
	}

	function searchValue(res){

		var highValue = [];
		var commonValue  = [];
		var lowValue = [];
		var time = [];

		CoreLoading.setState(false);
		if(res.state === '10000'){
			$scope.flagObj = true;
			angular.forEach(res.data, function(value){
				highValue.push(value.highValue);
				commonValue.push(value.commonValue);
				lowValue.push(value.lowValue);
				time.push(value.time);
			});
			var itemStyle =  {
				normal: { // 系列级个性化，横向渐变填充
					borderRadius: 5
				}
			};
			var option = {
				title: {
					text: '客户价值统计',
					x: 'center'
				},
				tooltip: {
					trigger: 'axis',
					axisPointer: {
						type: 'shadow'
					}
				},
				legend: {
					orient: 'vertical',
					x: 'right',
					y: 'center',
					data: ['高价值','普通价值','低价值']
				},
				// calculable: true,
				grid: {
					x2: '13%'
				},
				dataZoom : {show : true,realtime : true,start : 0,end : 100},
				xAxis: [
					{
						type: 'category',
						data: time
					}
				],
				yAxis: [
					{
						type: 'value',
						name:'(人数)'
					}
				],
				series: [
					{
						name: '高价值',
						type: 'bar',
						itemStyle: itemStyle,
						data: highValue
					},
					{
						name: '普通价值',
						type: 'bar',
						itemStyle: itemStyle,
						data: commonValue
					},
					{
						name: '低价值',
						type: 'bar',
						itemStyle: itemStyle,
						data: lowValue
					}
				]
			};
			var customerEchart = echarts.init(document.getElementById('value'));
			customerEchart.setOption(option);


		}else{
			CoreService.dialog('系统错误');
		}
	
	}
	$scope.searchValue();

}
})();
